{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.6 Pose的能量\n",
    "如果现在我们已有一个Pose，我们想评估这个蛋白质的能量评分，可以直接通过创建一个打分函数并对Pose的能量进行计算，</br>\n",
    "再可以通过pose中的energies对象将氨基酸残基的One-body, Two-body的能量信息列出，达到残基级别能量\"分解\"的目的。\n",
    "\n",
    "#### 1.6.1 对结构进行能量计算\n",
    "create_score_function函数可以用于快速创建一个打分函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "\n\nFile: /Volumes/MacintoshHD3/benchmark/W.fujii.release/rosetta.Fujii.release/_commits_/main/source/build/PyRosetta/macos/clang-9.0.0/python-3.6/release.serialization.thread/source/src/utility/keys/SmallKeyVector.hh:538\n[ ERROR ] UtilityExitException\nERROR: Assertion `active( key )` failed.\n\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-6a078ebb4690>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m## 创建标准打分函数\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mpyrosetta\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcreate_score_function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mscorefxn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcreate_score_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ref2015'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;31m# 对当前Pose中的构象进行能量计算\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mRuntimeError\u001b[0m: \n\nFile: /Volumes/MacintoshHD3/benchmark/W.fujii.release/rosetta.Fujii.release/_commits_/main/source/build/PyRosetta/macos/clang-9.0.0/python-3.6/release.serialization.thread/source/src/utility/keys/SmallKeyVector.hh:538\n[ ERROR ] UtilityExitException\nERROR: Assertion `active( key )` failed.\n\n"
     ]
    }
   ],
   "source": [
    "## 创建标准打分函数\n",
    "from pyrosetta import create_score_function\n",
    "scorefxn = create_score_function('ref2015')\n",
    "\n",
    "# 对当前Pose中的构象进行能量计算\n",
    "weighted_total_score = scorefxn(pose)\n",
    "print(weighted_total_score)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.6.2 能量信息\n",
    "Rosetta的能量是加权后的能量，每个能量项有自己的权重，通过energies获取的是能量项的原始结果（unweighted）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pose' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-5ff76d8486bf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 获取能量对象\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mscores\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpose\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menergies\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;31m# 获取1号残基的所有能量项的信息:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mscores\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'pose' is not defined"
     ]
    }
   ],
   "source": [
    "# 获取能量对象\n",
    "scores = pose.energies()\n",
    "\n",
    "# 获取1号残基的所有能量项的信息:\n",
    "print(scores.show(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rosetta中的Score项有许多，如fa_atr代表范德华吸引势力, fa_rep代表范德华排斥项, fa_elec代表静电项等。</br>\n",
    "比如通过ScoreType类下的属性，即可搜索到对应的能量项。</br>\n",
    "获取总能中的某一个项的值可以直接使用python的索引功能，十分方便:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取总fa_atr项能量项的得分结果:\n",
    "from pyrosetta.rosetta.core.scoring import ScoreType\n",
    "pose.energies().total_energies()[ScoreType.fa_atr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单独获得第5号氨基酸的fa_atr项能量得分:\n",
    "pose.energies().residue_total_energies(5)[ScoreType.fa_atr]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pyrosetta",
   "language": "python",
   "name": "pyrosetta"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
